package com.eightbears.beboy.adapter;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.entity.ShopListEntity;

import java.text.DecimalFormat;

public class ShopInfoAdapter extends BaseQuickAdapter<ShopListEntity.DataBean.ListBean, BaseViewHolder> {

    public static final String STATUS_RIORITY_SIGN = "1";
    public static final String STATUS_RIORITY_RECOMMEND = "3";

    public ShopInfoAdapter() {
        super(R.layout.item_shop_info, null);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, ShopListEntity.DataBean.ListBean listBean) {

        String title = (baseViewHolder.getLayoutPosition() + 1) + "." + listBean.getTitle();
        String common = String.format(mContext.getResources().getString(R.string.text_shop_common), listBean.getComment() + "");

        DecimalFormat df = new DecimalFormat("#.#");
        String distance = df.format(listBean.getDistance());
        if (Double.parseDouble(distance) <= Double.valueOf(0.5)) {
            baseViewHolder.setText(R.id.tv_shop_distance, "<500m");
        } else if (Double.parseDouble(distance) > Double.valueOf(0.5)) {
            baseViewHolder.setText(R.id.tv_shop_distance, distance + "km");
        }

        String sales = String.format(mContext.getResources().getString(R.string.text_shop_sale), listBean.getSales());
        showCommon(baseViewHolder);

        if (listBean.getHongbao().equals("1")) {
            showHongBao(baseViewHolder);
        }

        if (listBean.getPriority().equals(STATUS_RIORITY_RECOMMEND)) {
            showRecommend(baseViewHolder);
        } else if (listBean.getPriority().equals(STATUS_RIORITY_SIGN)) {
            showSign(baseViewHolder);
        }

        baseViewHolder.setText(R.id.tv_shop_name, title)
                .setText(R.id.tv_shop_address, listBean.getAddress())
                .setText(R.id.tv_shop_comment, common)
                .setRating(R.id.rb_start, (float) listBean.getScore())
                .setText(R.id.tv_shop_sale, sales)
                .addOnClickListener(R.id.shop_info_root);

        CommonUtils.loadImageView(mContext, listBean.getCover(), R.mipmap.icon_default_shop,
                (ImageView) baseViewHolder.getView(R.id.iv_shop_image));
    }

    private void showHongBao(BaseViewHolder baseViewHolder) {
        Glide.with(mContext).load(R.mipmap.gif_icon_honghao)
                .asGif().into((ImageView) baseViewHolder.getView(R.id.icon_hongbao));
        baseViewHolder.setVisible(R.id.icon_hongbao, true);
    }

    private void showRecommend(BaseViewHolder baseViewHolder) {
        baseViewHolder.setTextColor(R.id.tv_shop_name, mContext.getResources().getColor(R.color.text_shop_recommend));
        baseViewHolder.setVisible(R.id.tv_recommend, true);
    }

    private void showSign(BaseViewHolder baseViewHolder) {
        baseViewHolder.setTextColor(R.id.tv_shop_name, mContext.getResources().getColor(R.color.text_shop_sign));
    }

    private void showCommon(BaseViewHolder baseViewHolder) {
        baseViewHolder.setTextColor(R.id.tv_shop_name, mContext.getResources().getColor(R.color.text_main_color));
        baseViewHolder.setVisible(R.id.tv_recommend, false);
        baseViewHolder.setVisible(R.id.icon_hongbao, false);
    }

}
